var webpack = require('webpack');

var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var path = require('path');
var glob = require('glob');

var config = {
    resolve: {
        alias: {
            vue: 'vue/dist/vue.js'
        }
    },
    module: {
        // avoid webpack trying to shim process
        noParse: [/es6-promise\.js$/],
        loaders: [
            {
                test: /\.vue$/,
                loader: 'vue'
            },
            {
                test: /\.js$/,
                exclude: /node_modules|vue\/src|vue-router\/|vue-loader\/|vue-hot-reload-api\//,
                loader: 'babel'
            },
            // { test: /\.html/, loader: 'html-loader?compress=true'}, // 用了这个loader后，HtmlWebpackPlugin就将默认ejs改成这个了
            { test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader?sourceMap' ) },
            { test: /\.less$/, loader: 'style!css!less-loader?compress=true'},
            { test: /\.scss$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader!sass-loader') },
            { test: /\.json/, loader: 'json-loader'},
            { test: /\.(jpe?g|png|gif)$/i, loaders: [
                'url?limit=10000&name=../images/[hash:8].[name].[ext]'
            ]},
            {
                test: /\.(woff|woff2|ttf|eot|svg)([\?]?.*)$/,
                loader: 'url-loader?limit=10000&name=../fonts/[name].[ext]'
            }
        ]
    },
    plugins:[
        new webpack.ProvidePlugin({
            jQuery: "jquery",
            $: "jquery",
        }),
        new webpack.optimize.OccurenceOrderPlugin(),
    ],
    babel: {
        presets: ['es2015'],
        plugins: ['transform-runtime']
    }
};

// 获取所有入口文件
function getEntry(globPath, pathDir) {
    var files = glob.sync(globPath);
    var entries = {},
        entry, dirname, basename, pathname, extname;

    for (var i = 0; i < files.length; i++) {
        entry = files[i];
        dirname = path.dirname(entry);
        extname = path.extname(entry);
        basename = path.basename(entry, extname);
        pathname = path.join(dirname, basename);
        pathname = pathDir ? pathname.replace(new RegExp('^' + pathDir), '') : pathname;
        pathname = pathname.replace('/template', '');
        entries[pathname] = ['./' + entry];
    }
    return entries;
}



var chunks = Object.keys(getEntry('src/pages/*/*.ejs', 'src/pages/'));


// 生成HTML文件
chunks.forEach(function(pathname) {
    var conf = {
        title: 'My App',
        filename: '../pages/' + pathname + '.html',
        template: path.join(__dirname,'../../src/pages/'+ pathname +'/template.ejs'),
        inject: 'body',
        chunks: ['manifest', 'vendor', pathname],
        hash: true,
        minify: {
            removeComments: true,
            collapseWhitespace: false
        },
        // rootPath: '/'
    };
    config.plugins.push(new HtmlWebpackPlugin(conf));
});

module.exports = config;